﻿@model Lokad.Cloud.Console.WebRole.Models.Services.ServicesModel
@{ ViewBag.Title = "Lokad.Cloud Administration Console - Services"; }

<h1>Service Manager</h1>
<p>All initialized cloud services are listed below. Enable or disable a service by clicking its status icon.</p>

@if (Model.QueueServices.Any()) {
<h2>Queue Services</h2>
<table class="table">
	@foreach (var item in Model.QueueServices) {
		<tr>
			<td>@Html.StartStopIconButton(item.IsStarted, item.ServiceName)</td>
			<td><b>@item.ServiceName</b><br />
			<small>Messages: @item.Definition.MessageTypeName</small><br />
			<small>Queue: @item.Definition.QueueName</small></td>
		</tr>
	}
</table>
}

@if (Model.ScheduledServices.Any()) {
<h2>Scheduled Services</h2>
<table class="table">
	@foreach (var item in Model.ScheduledServices) {
		<tr>
			<td>@Html.StartStopIconButton(item.IsStarted, item.ServiceName)</td>
			<td>@item.ServiceName</td>
		</tr>
	}
</table>
}

@if (Model.CloudServices.Any()) {
<h2>Other Services</h2>
<table class="table">
	@foreach (var item in Model.CloudServices) {
		<tr>
			<td>@Html.StartStopIconButton(item.IsStarted, item.ServiceName)</td>
			<td>@item.ServiceName</td>
		</tr>
	}
</table>
}

@if (Model.UnavailableServices.Any()) {
<h2>Unavailable Services</h2>
<table class="table">
	@foreach (var item in Model.UnavailableServices) {
		<tr>
			<td>@Html.StartStopIconButton(item.IsStarted, item.ServiceName)</td>
			<td>@item.ServiceName</td>
		</tr>
	}
</table>
}

<script type="text/javascript">
	function statusToClass(status) {
		if (status) return '@IconMap.StartStopOf(true)';
		else return '@IconMap.StartStopOf(false)';
	}
	$(document).ready(function () {
		$('.icon-button').click(function () {
			$('#AjaxLoading').show();
			me = $(this);
			enable = me.hasClass(statusToClass(false));
			me.removeClass(statusToClass(!enable));
			me.addClass('@IconMap.Loading');
			$.ajax({
				url: '@ViewBag.TenantPath/Status/' + me.attr('id'),
				type: 'PUT',
				dataType: 'json',
				data: { isStarted: enable },
				cache: false,
				success: function (data) {
					element = $('span[id=' + data.serviceName + ']');
					element.removeClass('@IconMap.Loading');
					element.addClass(statusToClass(data.isStarted));
					$('#AjaxLoading').hide();
				}
			});
		});
	});
</script>
